package com.song.seckill.controller;

import com.song.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/seckill")
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @GetMapping(value = "/trySubmit")
    public String seckill(@RequestParam String skuId, @RequestParam String userId) {
        // 1. 校验用户是否重复购买
        if (!seckillService.checkRepeat(userId, skuId)) {
            return "请勿重复提交";
        }
        // 2. 执行秒杀
        if (seckillService.trySeckill(skuId, userId)) {
            // 3. 异步更新数据库
            boolean result = seckillService.asyncUpdateStock(skuId);
            if (!result) {
                //如何回退占用库存及占用用户是否购买的状态，还有秒杀已经成功了
                return "更新库存失败";
            }else{
                //下单 生成 order订单

                return "秒杀成功 - 返回订单，点击订单号去支付-获取返回一个秒杀凭证，通过秒杀凭证去支付";
            }
        }
        return "库存不足";
    }
}
